package chess;

import comm.*;

public class ChessBoard {

	public static final int ROWS = 15;
	public static final int COLS = 15;
	public static final int TOTAL = ROWS * COLS;
	public static final byte MARK = -2;
	public static final byte NONE = -1;
	public static final byte BLACK = 0;
	public static final byte WHITE = 1;

	public byte[][] board = new byte[ROWS][COLS];
	public byte turn;
	public int chessed;
	public Chess[] chessList = new Chess[TOTAL];
	public int remaining;

	public ChessBoard() {
		for (int i = 0; i < ROWS; i++)
			for (int j = 0; j < COLS; j++)
				board[i][j] = NONE;
		turn = BLACK;
		chessed = 0;
		remaining = TOTAL;
	}

	public ChessBoard(ChessBoard b) {
		for (int i = 0; i < ROWS; i++)
			for (int j = 0; j < COLS; j++)
				board[i][j] = b.board[i][j];
		turn = b.turn;
		chessed = b.chessed;
		for (int i = 0; i < chessed; i++)
			chessList[i] = new Chess(b.chessList[i]);
		remaining = b.remaining;
	}

	public void update(ChessInfo c) {
		board[c.row][c.col] = c.color;
		turn = (byte) (1 - turn);
		chessList[chessed] = new Chess(c.color, c.row, c.col);
		chessed++;
		remaining--;
	}

	public void update(Chess c) {
		board[c.row][c.col] = c.color;
		turn = (byte) (1 - turn);
		chessList[chessed] = new Chess(c);
		chessed++;
		remaining--;
	}

	public void update(byte r, byte c) {
		update(new Chess(turn, r, c));
	}

	public boolean isMark(int i, int j) {
		return (board[i][j] == MARK);
	}

	public boolean isNone(int i, int j) {
		return (board[i][j] == NONE);
	}

	public boolean isBlack(int i, int j) {
		return (board[i][j] == BLACK);
	}

	public boolean isWhite(int i, int j) {
		return (board[i][j] == WHITE);
	}

	public Byte color(int i, int j) {
		return board[i][j];
	}

	public void print() {
		for (int i = 0; i < ROWS; i++) {
			for (int j = 0; j < COLS; j++)
				switch (board[i][j]) {
				case NONE:
					System.out.print(". ");
					break;
				case BLACK:
					System.out.print("x ");
					break;
				case WHITE:
					System.out.print("o ");
					break;
				case MARK:
					System.out.print("? ");
					break;
				}
			System.out.println();
		}
	}

}
